package com.ztom.daily.n2022.m4;

import java.util.HashSet;
import java.util.Set;

/**
 * 唯一摩尔斯密码
 * <p>
 * https://leetcode-cn.com/problems/unique-morse-code-words/
 *
 * @author ZhangTao
 */
public class Day10UniqueMorseRepresentations {

    public static final String[] MORSE = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
            "....", "..", ".---", "-.-", ".-..", "--", "-.",
            "---", ".--.", "--.-", ".-.", "...", "-", "..-",
            "...-", ".--", "-..-", "-.--", "--.."};

    public int uniqueMorseRepresentations(String[] words) {
        if (words == null || words.length == 0) {
            return 0;
        }
        if (words.length == 1) {
            return 1;
        }

        Set<String> set = new HashSet<>();

        for (String word : words) {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < word.length(); i++) {
                char c = word.charAt(i);
                builder.append(MORSE[c - 'a']);
            }
            set.add(builder.toString());
        }
        return set.size();
    }
}
